package io.renren.modules.app.utils;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @Auther zxl
 * @Date 2018/12/15 12:09
 * @Description
 **/
public class FileUtils {


    /**
     * 上传文件
     * @param file
     * @param path
     * @param fileName
     * @return
     */
    public static boolean upload(MultipartFile file, String path, String fileName){

        String realPath = path + "/" + fileName;
        File dest = new File(realPath);
        //判断文件父目录是否存在
        if(!dest.getParentFile().exists()){
            dest.getParentFile().mkdir();
        }
        try {
            //保存文件
            System.out.println("realPath:"+realPath);
            file.transferTo(dest);
            return true;
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;
        }

    }
}
